package com.oracle.oa.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.oracle.oa.biz.AccessoryfileBiz;
import com.oracle.oa.biz.BaseBiz;
import com.oracle.oa.biz.FileinfoBiz;
import com.oracle.oa.biz.FiletypeinfoBiz;
import com.oracle.oa.entity.Accessoryfile;
import com.oracle.oa.entity.Departinfo;
import com.oracle.oa.entity.Fileinfo;
import com.oracle.oa.entity.Filetypeinfo;
import com.oracle.oa.entity.Userinfo;
import com.oracle.oa.util.Tree;

public class TreeAction extends ActionSupport implements SessionAware{

	private static final long serialVersionUID = 1L;
	private String html;
	public Map<String, Object> session=null;
	private long fileid;
	private String url;
	private Accessoryfile accessoryfile;
	private Fileinfo fileinfo;
	private String refresh;
	private List<Fileinfo> fileinfoList=new ArrayList<Fileinfo>();
	private List<Accessoryfile> accessoryfileList=new ArrayList<Accessoryfile>();
	BaseBiz bBiz;
	FileinfoBiz fBiz;
	AccessoryfileBiz aBiz;
	FiletypeinfoBiz ftBiz;
	public String out(){
		if(!geturl().equals("admin_cg")){
			String departid=((Userinfo)(session.get("userinfo"))).getDepartid().getDepartid()+"";
			String hql="from Departinfo d where d.departname!='"+departid+"'";
			List<Departinfo> dList=fBiz.getAllByHQL(hql);
			System.out.println(dList.size());
			for(Departinfo d:dList){
				if(fBiz.getByProperty(Fileinfo.class, "filename", d.getDepartname()).size()>0){
					fileinfo=(Fileinfo) (fBiz.getByProperty(Fileinfo.class, "filename", d.getDepartname())).get(0);
					fBiz.toRecycleBin(fileinfo.getFileid(), 0l);
				}
			}
		}
		fBiz.toRecycleBin(fileid, 0l);
		url="../Login.jsp";
		return SUCCESS;
	}
	 public String geturl(){
			if(session.get("url")!=null){
				url=session.get("url").toString();
			}else{
				url="";
			}
			return url;
		}
	public String menu() {
		session.put("where", 1);
		List<Fileinfo> nodes = new ArrayList<Fileinfo>();
        //nodes=bBiz.getAll(Fileinfo.class);
		
		if(!geturl().equals("admin_cg")){
			String departid=((Userinfo)(session.get("userinfo"))).getDepartid().getDepartid()+"";
			String hql="from Departinfo d where d.departname!='"+departid+"'";
			List<Departinfo> dList=fBiz.getAllByHQL(hql);
			System.out.println(dList.size());
			for(Departinfo d:dList){
				if(fBiz.getByProperty(Fileinfo.class, "filename", d.getDepartname()).size()>0){
					fileinfo=(Fileinfo) (fBiz.getByProperty(Fileinfo.class, "filename", d.getDepartname())).get(0);
					fBiz.toRecycleBin(fileinfo.getFileid(), 1l);
				}
				
				
			}
		}
		nodes=bBiz.getByProperty(Fileinfo.class, "ifdelete", 0l);
		Tree tree = new Tree(nodes);
		html = tree.buildTree();
		session.put("html", html);
		
		if(refresh!=null)
			url="/"+geturl()+"/fileManage/treeHtml.jsp";
		else
			url="/"+geturl()+"/fileManage/fileMenu.jsp";
		return SUCCESS;
	}
	
	public String findChild(){
		if(session.get("fileid")!=null){
			fileid=(Long) session.get("fileid");
			session.remove("fileid");
		}
		if(fileid<1l)
			fileid=1l;
		    
			fileinfo=(Fileinfo) fBiz.getById(Fileinfo.class,fileid);
			if(fileinfo.getFiletypeinfo().getFiletypeid()==1l){
				fileinfoList=fBiz.findFileChild(fileid);
				url="/"+geturl()+"/fileManage/findChildResult.jsp";
			}else{
				accessoryfile=(Accessoryfile) aBiz.findAccfile(fileid);
				url="/"+geturl()+"/fileManage/findAccDetity.jsp";
			}
		
		return SUCCESS;
	}

	public String getHtml() {
		return html;
	}

	public void setHtml(String html) {
		this.html = html;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public long getFileid() {
		return fileid;
	}

	public void setFileid(long fileid) {
		this.fileid = fileid;
	}

	public void setSession(Map<String, Object> arg0) {
		this.session=arg0;	
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public List<Fileinfo> getFileinfoList() {
		return fileinfoList;
	}

	public void setFileinfoList(List<Fileinfo> fileinfoList) {
		this.fileinfoList = fileinfoList;
	}

	public List<Accessoryfile> getAccessoryfileList() {
		return accessoryfileList;
	}

	public void setAccessoryfileList(List<Accessoryfile> accessoryfileList) {
		this.accessoryfileList = accessoryfileList;
	}

	public Accessoryfile getAccessoryfile() {
		return accessoryfile;
	}

	public void setAccessoryfile(Accessoryfile accessoryfile) {
		this.accessoryfile = accessoryfile;
	}

	public Fileinfo getFileinfo() {
		return fileinfo;
	}

	public void setFileinfo(Fileinfo fileinfo) {
		this.fileinfo = fileinfo;
	}

	public BaseBiz getbBiz() {
		return bBiz;
	}

	public void setbBiz(BaseBiz bBiz) {
		this.bBiz = bBiz;
	}

	public FileinfoBiz getfBiz() {
		return fBiz;
	}

	public void setfBiz(FileinfoBiz fBiz) {
		this.fBiz = fBiz;
	}

	public AccessoryfileBiz getaBiz() {
		return aBiz;
	}

	public void setaBiz(AccessoryfileBiz aBiz) {
		this.aBiz = aBiz;
	}

	public FiletypeinfoBiz getFtBiz() {
		return ftBiz;
	}

	public void setFtBiz(FiletypeinfoBiz ftBiz) {
		this.ftBiz = ftBiz;
	}

	public String getRefresh() {
		return refresh;
	}

	public void setRefresh(String refresh) {
		this.refresh = refresh;
	}
	
	

	
	
}
